<template>
  <div class="dialog-container">
    <el-dialog
      v-model="dialogVisible"
      :before-close="handleClose"
      :title="title"
      width="40%"
    >
      <el-form
        ref="formRef"
        label-position="left"
        label-width="100px"
        :model="data"
        :rules="rules"
      >
        <el-form-item label="背景名称" prop="CSTempImgName">
          <el-input
            v-model="data.CSTempImgName"
            placeholder="请输入背景名称"
            style="width: 250px"
          />
        </el-form-item>
        <el-form-item label="背景编码" prop="CSTempImgCode">
          <el-input
            v-model="data.CSTempImgCode"
            :disabled="type === 'edit'"
            placeholder="英文或数字"
            style="width: 250px"
          />
        </el-form-item>
        <el-form-item label="背景类型" prop="LCDType">
          <el-select v-model="data.LCDType" placeholder="请选择">
            <el-option
              v-for="item in typeList"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="状态">
          <el-switch
            v-model="data.Status"
            :active-value="1"
            :inactive-value="0"
          />
        </el-form-item>
        <el-form-item label="图片">
          <ImgUpload
            action="/CSLogoImage/CSLogoUpload?code=cstempimg"
            :image-url="data.CSTempImgUrl"
            @success="uploadSuccess"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="handleClose">取消</el-button>
          <el-button type="primary" @click="confirm">确定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script lang="ts" setup>
  import { ref } from 'vue'
  import ImgUpload from '@/views/components/imgUpload/index.vue'

  const validatePass = (rule: any, value: string, callback: any) => {
    if (!value) {
      callback(new Error('请输入编码'))
    } else if (!/^[A-Za-z0-9]+$/.test(value)) {
      callback(new Error('只能请输入英文和数字编码'))
    } else {
      callback()
    }
  }
  const rules = reactive({
    CSTempImgCode: [
      {
        required: true,
        validator: validatePass,
        trigger: 'blur',
      },
    ],
    CSTempImgName: [
      { required: true, trigger: 'blur', message: '请输入图片名称' },
    ],
    LCDType: [{ required: true, trigger: 'change', message: '请选择屏幕类型' }],
  })

  const props = defineProps({
    visible: {
      type: Boolean,
      default: false,
    },
    title: {
      type: String,
      default: '添加商品',
    },
    type: {
      type: String,
      default: 'add',
    },
    dialogData: {
      type: Object,
      default: () => {},
    },
  })
  const dialogVisible = ref(props.visible)
  const data: any = ref(JSON.parse(JSON.stringify(props.dialogData)))
  watch(
    () => props.visible,
    () => {
      dialogVisible.value = props.visible
      if (props.visible) {
        data.value = JSON.parse(JSON.stringify(props.dialogData))
      }
    }
  )
  const typeList = [
    {
      value: '',
      label: '全部',
    },
    {
      value: '00',
      label: '600*1024',
    },
    {
      value: '01',
      label: '600*664',
    },
    {
      value: '02',
      label: '600*620',
    },
    {
      value: '10',
      label: '1024*600',
    },
    {
      value: '11',
      label: '664*600',
    },
    {
      value: '12',
      label: '620*600',
    },
  ]
  const emit = defineEmits(['close', 'confirm'])
  const handleClose = () => {
    dialogVisible.value = false
    emit('close', dialogVisible.value)
  }
  const formRef: any = ref(null)
  const confirm = async () => {
    await formRef.value.validate(async (valid: any) => {
      if (valid) {
        emit('confirm', data.value)
      }
    })
    // dialogVisible.value = false
  }
  const uploadSuccess = (url: string) => {
    data.value.CSTempImgUrl = url
  }
</script>

<style lang="scss" scoped>
  .table {
  }

  .tag {
    font-size: 14px;
  }
</style>
